<html>
  <head>
  <title>graphicsUtils.py</title>
  </head>
  <body>
  <h3>graphicsUtils.py (<a href="../graphicsUtils.py">original</a>)</h3>
  <hr>
  <pre>
<span style="color: green; font-style: italic"># graphicsUtils.py
# ----------------
# Licensing Information: Please do not distribute or publish solutions to this
# project. You are free to use and extend these projects for educational
# purposes. The Pacman AI projects were developed at UC Berkeley, primarily by
# John DeNero (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu).
# For more info, see http://inst.eecs.berkeley.edu/~cs188/sp09/pacman.html

</span><span style="color: blue; font-weight: bold">import </span>sys
<span style="color: blue; font-weight: bold">import </span>math
<span style="color: blue; font-weight: bold">import </span>random
<span style="color: blue; font-weight: bold">import </span>string
<span style="color: blue; font-weight: bold">import </span>time
<span style="color: blue; font-weight: bold">import </span>types
<span style="color: blue; font-weight: bold">import </span>Tkinter

_Windows <span style="font-weight: bold">= </span>sys<span style="font-weight: bold">.</span>platform <span style="font-weight: bold">== </span><span style="color: red">'win32'  </span><span style="color: green; font-style: italic"># True if on Win95/98/NT

</span>_root_window <span style="font-weight: bold">= </span><span style="color: blue">None      </span><span style="color: green; font-style: italic"># The root window for graphics output
</span>_canvas <span style="font-weight: bold">= </span><span style="color: blue">None      </span><span style="color: green; font-style: italic"># The canvas which holds graphics
</span>_canvas_xs <span style="font-weight: bold">= </span><span style="color: blue">None      </span><span style="color: green; font-style: italic"># Size of canvas object
</span>_canvas_ys <span style="font-weight: bold">= </span><span style="color: blue">None
</span>_canvas_x <span style="font-weight: bold">= </span><span style="color: blue">None      </span><span style="color: green; font-style: italic"># Current position on canvas
</span>_canvas_y <span style="font-weight: bold">= </span><span style="color: blue">None
</span>_canvas_col <span style="font-weight: bold">= </span><span style="color: blue">None      </span><span style="color: green; font-style: italic"># Current colour (set to black below)
</span>_canvas_tsize <span style="font-weight: bold">= </span><span style="color: red">12
</span>_canvas_tserifs <span style="font-weight: bold">= </span><span style="color: red">0

</span><span style="color: blue; font-weight: bold">def </span>formatColor<span style="font-weight: bold">(</span>r<span style="font-weight: bold">, </span>g<span style="font-weight: bold">, </span>b<span style="font-weight: bold">):
  </span><span style="color: blue; font-weight: bold">return </span><span style="color: red">'#%02x%02x%02x' </span><span style="font-weight: bold">% (</span>int<span style="font-weight: bold">(</span>r <span style="font-weight: bold">* </span><span style="color: red">255</span><span style="font-weight: bold">), </span>int<span style="font-weight: bold">(</span>g <span style="font-weight: bold">* </span><span style="color: red">255</span><span style="font-weight: bold">), </span>int<span style="font-weight: bold">(</span>b <span style="font-weight: bold">* </span><span style="color: red">255</span><span style="font-weight: bold">))

</span><span style="color: blue; font-weight: bold">def </span>colorToVector<span style="font-weight: bold">(</span>color<span style="font-weight: bold">):
  </span><span style="color: blue; font-weight: bold">return </span>map<span style="font-weight: bold">(</span><span style="color: blue; font-weight: bold">lambda </span>x<span style="font-weight: bold">: </span>int<span style="font-weight: bold">(</span>x<span style="font-weight: bold">, </span><span style="color: red">16</span><span style="font-weight: bold">) / </span><span style="color: red">256.0</span><span style="font-weight: bold">, [</span>color<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">:</span><span style="color: red">3</span><span style="font-weight: bold">], </span>color<span style="font-weight: bold">[</span><span style="color: red">3</span><span style="font-weight: bold">:</span><span style="color: red">5</span><span style="font-weight: bold">], </span>color<span style="font-weight: bold">[</span><span style="color: red">5</span><span style="font-weight: bold">:</span><span style="color: red">7</span><span style="font-weight: bold">]])

</span><span style="color: blue; font-weight: bold">if </span>_Windows<span style="font-weight: bold">:
    </span>_canvas_tfonts <span style="font-weight: bold">= [</span><span style="color: red">'times new roman'</span><span style="font-weight: bold">, </span><span style="color: red">'lucida console'</span><span style="font-weight: bold">]
</span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
    </span>_canvas_tfonts <span style="font-weight: bold">= [</span><span style="color: red">'times'</span><span style="font-weight: bold">, </span><span style="color: red">'lucidasans-24'</span><span style="font-weight: bold">]
    </span><span style="color: blue; font-weight: bold">pass </span><span style="color: green; font-style: italic"># XXX need defaults here

</span><span style="color: blue; font-weight: bold">def </span>sleep<span style="font-weight: bold">(</span>secs<span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">global </span>_root_window
    <span style="color: blue; font-weight: bold">if </span>_root_window <span style="font-weight: bold">== </span><span style="color: blue">None</span><span style="font-weight: bold">:
        </span>time<span style="font-weight: bold">.</span>sleep<span style="font-weight: bold">(</span>secs<span style="font-weight: bold">)
    </span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
        </span>_root_window<span style="font-weight: bold">.</span>update_idletasks<span style="font-weight: bold">()
        </span>_root_window<span style="font-weight: bold">.</span>after<span style="font-weight: bold">(</span>int<span style="font-weight: bold">(</span><span style="color: red">1000 </span><span style="font-weight: bold">* </span>secs<span style="font-weight: bold">), </span>_root_window<span style="font-weight: bold">.</span>quit<span style="font-weight: bold">)
        </span>_root_window<span style="font-weight: bold">.</span>mainloop<span style="font-weight: bold">()

</span><span style="color: blue; font-weight: bold">def </span>begin_graphics<span style="font-weight: bold">(</span>width<span style="font-weight: bold">=</span><span style="color: red">640</span><span style="font-weight: bold">, </span>height<span style="font-weight: bold">=</span><span style="color: red">480</span><span style="font-weight: bold">, </span>color<span style="font-weight: bold">=</span>formatColor<span style="font-weight: bold">(</span><span style="color: red">0</span><span style="font-weight: bold">, </span><span style="color: red">0</span><span style="font-weight: bold">, </span><span style="color: red">0</span><span style="font-weight: bold">), </span>title<span style="font-weight: bold">=</span><span style="color: blue">None</span><span style="font-weight: bold">):

    </span><span style="color: blue; font-weight: bold">global </span>_root_window<span style="font-weight: bold">, </span>_canvas<span style="font-weight: bold">, </span>_canvas_x<span style="font-weight: bold">, </span>_canvas_y<span style="font-weight: bold">, </span>_canvas_xs<span style="font-weight: bold">, </span>_canvas_ys<span style="font-weight: bold">, </span>_bg_color

    <span style="color: green; font-style: italic"># Check for duplicate call
    </span><span style="color: blue; font-weight: bold">if </span>_root_window <span style="color: blue; font-weight: bold">is not </span><span style="color: blue">None</span><span style="font-weight: bold">:
        </span><span style="color: green; font-style: italic"># Lose the window.
        </span>_root_window<span style="font-weight: bold">.</span>destroy<span style="font-weight: bold">()
        
    </span><span style="color: green; font-style: italic"># Save the canvas size parameters
    </span>_canvas_xs<span style="font-weight: bold">, </span>_canvas_ys <span style="font-weight: bold">= </span>width <span style="font-weight: bold">- </span><span style="color: red">1</span><span style="font-weight: bold">, </span>height <span style="font-weight: bold">- </span><span style="color: red">1
    </span>_canvas_x<span style="font-weight: bold">, </span>_canvas_y <span style="font-weight: bold">= </span><span style="color: red">0</span><span style="font-weight: bold">, </span>_canvas_ys
    _bg_color <span style="font-weight: bold">= </span>color
    
    <span style="color: green; font-style: italic"># Create the root window
    </span>_root_window <span style="font-weight: bold">= </span>Tkinter<span style="font-weight: bold">.</span>Tk<span style="font-weight: bold">()
    </span>_root_window<span style="font-weight: bold">.</span>protocol<span style="font-weight: bold">(</span><span style="color: red">'WM_DELETE_WINDOW'</span><span style="font-weight: bold">, </span>_destroy_window<span style="font-weight: bold">)
    </span>_root_window<span style="font-weight: bold">.</span>title<span style="font-weight: bold">(</span>title <span style="color: blue; font-weight: bold">or </span><span style="color: red">'Graphics Window'</span><span style="font-weight: bold">)
    </span>_root_window<span style="font-weight: bold">.</span>resizable<span style="font-weight: bold">(</span><span style="color: red">0</span><span style="font-weight: bold">, </span><span style="color: red">0</span><span style="font-weight: bold">)

    </span><span style="color: green; font-style: italic"># Create the canvas object
    </span><span style="color: blue; font-weight: bold">try</span><span style="font-weight: bold">:
      </span>_canvas <span style="font-weight: bold">= </span>Tkinter<span style="font-weight: bold">.</span>Canvas<span style="font-weight: bold">(</span>_root_window<span style="font-weight: bold">, </span>width<span style="font-weight: bold">=</span>width<span style="font-weight: bold">, </span>height<span style="font-weight: bold">=</span>height<span style="font-weight: bold">)
      </span>_canvas<span style="font-weight: bold">.</span>pack<span style="font-weight: bold">()
      </span>draw_background<span style="font-weight: bold">()
      </span>_canvas<span style="font-weight: bold">.</span>update<span style="font-weight: bold">()
    </span><span style="color: blue; font-weight: bold">except</span><span style="font-weight: bold">:
      </span>_root_window <span style="font-weight: bold">= </span><span style="color: blue">None
      </span><span style="color: blue; font-weight: bold">raise

    </span><span style="color: green; font-style: italic"># Bind to key-down and key-up events
    </span>_root_window<span style="font-weight: bold">.</span>bind<span style="font-weight: bold">( </span><span style="color: red">"&lt;KeyPress&gt;"</span><span style="font-weight: bold">, </span>_keypress <span style="font-weight: bold">)
    </span>_root_window<span style="font-weight: bold">.</span>bind<span style="font-weight: bold">( </span><span style="color: red">"&lt;KeyRelease&gt;"</span><span style="font-weight: bold">, </span>_keyrelease <span style="font-weight: bold">)
    </span>_root_window<span style="font-weight: bold">.</span>bind<span style="font-weight: bold">( </span><span style="color: red">"&lt;FocusIn&gt;"</span><span style="font-weight: bold">, </span>_clear_keys <span style="font-weight: bold">)
    </span>_root_window<span style="font-weight: bold">.</span>bind<span style="font-weight: bold">( </span><span style="color: red">"&lt;FocusOut&gt;"</span><span style="font-weight: bold">, </span>_clear_keys <span style="font-weight: bold">)
    </span>_root_window<span style="font-weight: bold">.</span>bind<span style="font-weight: bold">( </span><span style="color: red">"&lt;Button-1&gt;"</span><span style="font-weight: bold">, </span>_leftclick <span style="font-weight: bold">)
    </span>_root_window<span style="font-weight: bold">.</span>bind<span style="font-weight: bold">( </span><span style="color: red">"&lt;Button-2&gt;"</span><span style="font-weight: bold">, </span>_rightclick <span style="font-weight: bold">)
    </span>_root_window<span style="font-weight: bold">.</span>bind<span style="font-weight: bold">( </span><span style="color: red">"&lt;Button-3&gt;"</span><span style="font-weight: bold">, </span>_rightclick <span style="font-weight: bold">)
    </span>_root_window<span style="font-weight: bold">.</span>bind<span style="font-weight: bold">( </span><span style="color: red">"&lt;Control-Button-1&gt;"</span><span style="font-weight: bold">, </span>_ctrl_leftclick<span style="font-weight: bold">)
    </span>_clear_keys<span style="font-weight: bold">()

</span>_leftclick_loc <span style="font-weight: bold">= </span><span style="color: blue">None
</span>_rightclick_loc <span style="font-weight: bold">= </span><span style="color: blue">None
</span>_ctrl_leftclick_loc <span style="font-weight: bold">= </span><span style="color: blue">None

</span><span style="color: blue; font-weight: bold">def </span>_leftclick<span style="font-weight: bold">(</span>event<span style="font-weight: bold">):
  </span><span style="color: blue; font-weight: bold">global </span>_leftclick_loc
  _leftclick_loc <span style="font-weight: bold">= (</span>event<span style="font-weight: bold">.</span>x<span style="font-weight: bold">, </span>event<span style="font-weight: bold">.</span>y<span style="font-weight: bold">)

</span><span style="color: blue; font-weight: bold">def </span>_rightclick<span style="font-weight: bold">(</span>event<span style="font-weight: bold">):
  </span><span style="color: blue; font-weight: bold">global </span>_rightclick_loc
  _rightclick_loc <span style="font-weight: bold">= (</span>event<span style="font-weight: bold">.</span>x<span style="font-weight: bold">, </span>event<span style="font-weight: bold">.</span>y<span style="font-weight: bold">)

</span><span style="color: blue; font-weight: bold">def </span>_ctrl_leftclick<span style="font-weight: bold">(</span>event<span style="font-weight: bold">):
  </span><span style="color: blue; font-weight: bold">global </span>_ctrl_leftclick_loc
  _ctrl_leftclick_loc <span style="font-weight: bold">= (</span>event<span style="font-weight: bold">.</span>x<span style="font-weight: bold">, </span>event<span style="font-weight: bold">.</span>y<span style="font-weight: bold">)

</span><span style="color: blue; font-weight: bold">def </span>wait_for_click<span style="font-weight: bold">():
  </span><span style="color: blue; font-weight: bold">while True</span><span style="font-weight: bold">:
    </span><span style="color: blue; font-weight: bold">global </span>_leftclick_loc
    <span style="color: blue; font-weight: bold">global </span>_rightclick_loc
    <span style="color: blue; font-weight: bold">global </span>_ctrl_leftclick_loc
    <span style="color: blue; font-weight: bold">if </span>_leftclick_loc <span style="font-weight: bold">!= </span><span style="color: blue">None</span><span style="font-weight: bold">:
      </span>val <span style="font-weight: bold">= </span>_leftclick_loc
      _leftclick_loc <span style="font-weight: bold">= </span><span style="color: blue">None
      </span><span style="color: blue; font-weight: bold">return </span>val<span style="font-weight: bold">, </span><span style="color: red">'left'
    </span><span style="color: blue; font-weight: bold">if </span>_rightclick_loc <span style="font-weight: bold">!= </span><span style="color: blue">None</span><span style="font-weight: bold">:
      </span>val <span style="font-weight: bold">= </span>_rightclick_loc
      _rightclick_loc <span style="font-weight: bold">= </span><span style="color: blue">None
      </span><span style="color: blue; font-weight: bold">return </span>val<span style="font-weight: bold">, </span><span style="color: red">'right'
    </span><span style="color: blue; font-weight: bold">if </span>_ctrl_leftclick_loc <span style="font-weight: bold">!= </span><span style="color: blue">None</span><span style="font-weight: bold">:
      </span>val <span style="font-weight: bold">= </span>_ctrl_leftclick_loc
      _ctrl_leftclick_loc <span style="font-weight: bold">= </span><span style="color: blue">None
      </span><span style="color: blue; font-weight: bold">return </span>val<span style="font-weight: bold">, </span><span style="color: red">'ctrl_left'
    </span>sleep<span style="font-weight: bold">(</span><span style="color: red">0.05</span><span style="font-weight: bold">)  

</span><span style="color: blue; font-weight: bold">def </span>draw_background<span style="font-weight: bold">():
    </span>corners <span style="font-weight: bold">= [(</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">), (</span><span style="color: red">0</span><span style="font-weight: bold">, </span>_canvas_ys<span style="font-weight: bold">), (</span>_canvas_xs<span style="font-weight: bold">, </span>_canvas_ys<span style="font-weight: bold">), (</span>_canvas_xs<span style="font-weight: bold">, </span><span style="color: red">0</span><span style="font-weight: bold">)]
    </span>polygon<span style="font-weight: bold">(</span>corners<span style="font-weight: bold">, </span>_bg_color<span style="font-weight: bold">, </span>fillColor<span style="font-weight: bold">=</span>_bg_color<span style="font-weight: bold">, </span>filled<span style="font-weight: bold">=</span><span style="color: blue; font-weight: bold">True</span><span style="font-weight: bold">, </span>smoothed<span style="font-weight: bold">=</span><span style="color: blue; font-weight: bold">False</span><span style="font-weight: bold">)
    
</span><span style="color: blue; font-weight: bold">def </span>_destroy_window<span style="font-weight: bold">(</span>event<span style="font-weight: bold">=</span><span style="color: blue">None</span><span style="font-weight: bold">):
    </span>sys<span style="font-weight: bold">.</span>exit<span style="font-weight: bold">(</span><span style="color: red">0</span><span style="font-weight: bold">)
</span><span style="color: green; font-style: italic">#    global _root_window
#    _root_window.destroy()
#    _root_window = None
    #print "DESTROY"

</span><span style="color: blue; font-weight: bold">def </span>end_graphics<span style="font-weight: bold">():
    </span><span style="color: blue; font-weight: bold">global </span>_root_window<span style="font-weight: bold">, </span>_canvas<span style="font-weight: bold">, </span>_mouse_enabled
    <span style="color: blue; font-weight: bold">try</span><span style="font-weight: bold">:
      </span><span style="color: blue; font-weight: bold">try</span><span style="font-weight: bold">:
        </span>sleep<span style="font-weight: bold">(</span><span style="color: red">1</span><span style="font-weight: bold">)
        </span><span style="color: blue; font-weight: bold">if </span>_root_window <span style="font-weight: bold">!= </span><span style="color: blue">None</span><span style="font-weight: bold">: 
          </span>_root_window<span style="font-weight: bold">.</span>destroy<span style="font-weight: bold">()
      </span><span style="color: blue; font-weight: bold">except </span>SystemExit<span style="font-weight: bold">, </span>e<span style="font-weight: bold">:
        </span><span style="color: blue; font-weight: bold">print </span><span style="color: red">'Ending graphics raised an exception:'</span><span style="font-weight: bold">, </span>e
    <span style="color: blue; font-weight: bold">finally</span><span style="font-weight: bold">:
      </span>_root_window <span style="font-weight: bold">= </span><span style="color: blue">None
      </span>_canvas <span style="font-weight: bold">= </span><span style="color: blue">None
      </span>_mouse_enabled <span style="font-weight: bold">= </span><span style="color: red">0
      </span>_clear_keys<span style="font-weight: bold">()

</span><span style="color: blue; font-weight: bold">def </span>clear_screen<span style="font-weight: bold">(</span>background<span style="font-weight: bold">=</span><span style="color: blue">None</span><span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">global </span>_canvas_x<span style="font-weight: bold">, </span>_canvas_y
    _canvas<span style="font-weight: bold">.</span>delete<span style="font-weight: bold">(</span><span style="color: red">'all'</span><span style="font-weight: bold">)
    </span>draw_background<span style="font-weight: bold">()
    </span>_canvas_x<span style="font-weight: bold">, </span>_canvas_y <span style="font-weight: bold">= </span><span style="color: red">0</span><span style="font-weight: bold">, </span>_canvas_ys

<span style="color: blue; font-weight: bold">def </span>polygon<span style="font-weight: bold">(</span>coords<span style="font-weight: bold">, </span>outlineColor<span style="font-weight: bold">, </span>fillColor<span style="font-weight: bold">=</span><span style="color: blue">None</span><span style="font-weight: bold">, </span>filled<span style="font-weight: bold">=</span><span style="color: red">1</span><span style="font-weight: bold">, </span>smoothed<span style="font-weight: bold">=</span><span style="color: red">1</span><span style="font-weight: bold">, </span>behind<span style="font-weight: bold">=</span><span style="color: red">0</span><span style="font-weight: bold">, </span>width<span style="font-weight: bold">=</span><span style="color: red">1</span><span style="font-weight: bold">):
  </span>c <span style="font-weight: bold">= []
  </span><span style="color: blue; font-weight: bold">for </span>coord <span style="color: blue; font-weight: bold">in </span>coords<span style="font-weight: bold">:
    </span>c<span style="font-weight: bold">.</span>append<span style="font-weight: bold">(</span>coord<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">])
    </span>c<span style="font-weight: bold">.</span>append<span style="font-weight: bold">(</span>coord<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">])
  </span><span style="color: blue; font-weight: bold">if </span>fillColor <span style="font-weight: bold">== </span><span style="color: blue">None</span><span style="font-weight: bold">: </span>fillColor <span style="font-weight: bold">= </span>outlineColor
  <span style="color: blue; font-weight: bold">if </span>filled <span style="font-weight: bold">== </span><span style="color: red">0</span><span style="font-weight: bold">: </span>fillColor <span style="font-weight: bold">= </span><span style="color: red">""
  </span>poly <span style="font-weight: bold">= </span>_canvas<span style="font-weight: bold">.</span>create_polygon<span style="font-weight: bold">(</span>c<span style="font-weight: bold">, </span>outline<span style="font-weight: bold">=</span>outlineColor<span style="font-weight: bold">, </span>fill<span style="font-weight: bold">=</span>fillColor<span style="font-weight: bold">, </span>smooth<span style="font-weight: bold">=</span>smoothed<span style="font-weight: bold">, </span>width<span style="font-weight: bold">=</span>width<span style="font-weight: bold">)
  </span><span style="color: blue; font-weight: bold">if </span>behind <span style="font-weight: bold">&gt; </span><span style="color: red">0</span><span style="font-weight: bold">:    
    </span>_canvas<span style="font-weight: bold">.</span>tag_lower<span style="font-weight: bold">(</span>poly<span style="font-weight: bold">, </span>behind<span style="font-weight: bold">) </span><span style="color: green; font-style: italic"># Higher should be more visible
  </span><span style="color: blue; font-weight: bold">return </span>poly
  
<span style="color: blue; font-weight: bold">def </span>square<span style="font-weight: bold">(</span>pos<span style="font-weight: bold">, </span>r<span style="font-weight: bold">, </span>color<span style="font-weight: bold">, </span>filled<span style="font-weight: bold">=</span><span style="color: red">1</span><span style="font-weight: bold">, </span>behind<span style="font-weight: bold">=</span><span style="color: red">0</span><span style="font-weight: bold">):
  </span>x<span style="font-weight: bold">, </span>y <span style="font-weight: bold">= </span>pos
  coords <span style="font-weight: bold">= [(</span>x <span style="font-weight: bold">- </span>r<span style="font-weight: bold">, </span>y <span style="font-weight: bold">- </span>r<span style="font-weight: bold">), (</span>x <span style="font-weight: bold">+ </span>r<span style="font-weight: bold">, </span>y <span style="font-weight: bold">- </span>r<span style="font-weight: bold">), (</span>x <span style="font-weight: bold">+ </span>r<span style="font-weight: bold">, </span>y <span style="font-weight: bold">+ </span>r<span style="font-weight: bold">), (</span>x <span style="font-weight: bold">- </span>r<span style="font-weight: bold">, </span>y <span style="font-weight: bold">+ </span>r<span style="font-weight: bold">)]
  </span><span style="color: blue; font-weight: bold">return </span>polygon<span style="font-weight: bold">(</span>coords<span style="font-weight: bold">, </span>color<span style="font-weight: bold">, </span>color<span style="font-weight: bold">, </span>filled<span style="font-weight: bold">, </span><span style="color: red">0</span><span style="font-weight: bold">, </span>behind<span style="font-weight: bold">=</span>behind<span style="font-weight: bold">)

</span><span style="color: blue; font-weight: bold">def </span>circle<span style="font-weight: bold">(</span>pos<span style="font-weight: bold">, </span>r<span style="font-weight: bold">, </span>outlineColor<span style="font-weight: bold">, </span>fillColor<span style="font-weight: bold">, </span>endpoints<span style="font-weight: bold">=</span><span style="color: blue">None</span><span style="font-weight: bold">, </span>style<span style="font-weight: bold">=</span><span style="color: red">'pieslice'</span><span style="font-weight: bold">, </span>width<span style="font-weight: bold">=</span><span style="color: red">2</span><span style="font-weight: bold">):
    </span>x<span style="font-weight: bold">, </span>y <span style="font-weight: bold">= </span>pos
    x0<span style="font-weight: bold">, </span>x1 <span style="font-weight: bold">= </span>x <span style="font-weight: bold">- </span>r <span style="font-weight: bold">- </span><span style="color: red">1</span><span style="font-weight: bold">, </span>x <span style="font-weight: bold">+ </span>r
    y0<span style="font-weight: bold">, </span>y1 <span style="font-weight: bold">= </span>y <span style="font-weight: bold">- </span>r <span style="font-weight: bold">- </span><span style="color: red">1</span><span style="font-weight: bold">, </span>y <span style="font-weight: bold">+ </span>r
    <span style="color: blue; font-weight: bold">if </span>endpoints <span style="font-weight: bold">== </span><span style="color: blue">None</span><span style="font-weight: bold">:
      </span>e <span style="font-weight: bold">= [</span><span style="color: red">0</span><span style="font-weight: bold">, </span><span style="color: red">359</span><span style="font-weight: bold">]
    </span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
      </span>e <span style="font-weight: bold">= </span>list<span style="font-weight: bold">(</span>endpoints<span style="font-weight: bold">)
    </span><span style="color: blue; font-weight: bold">while </span>e<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">] &gt; </span>e<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">]: </span>e<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">] = </span>e<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">] + </span><span style="color: red">360

    </span><span style="color: blue; font-weight: bold">return </span>_canvas<span style="font-weight: bold">.</span>create_arc<span style="font-weight: bold">(</span>x0<span style="font-weight: bold">, </span>y0<span style="font-weight: bold">, </span>x1<span style="font-weight: bold">, </span>y1<span style="font-weight: bold">, </span>outline<span style="font-weight: bold">=</span>outlineColor<span style="font-weight: bold">, </span>fill<span style="font-weight: bold">=</span>fillColor<span style="font-weight: bold">,
                              </span>extent<span style="font-weight: bold">=</span>e<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">] - </span>e<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">], </span>start<span style="font-weight: bold">=</span>e<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">], </span>style<span style="font-weight: bold">=</span>style<span style="font-weight: bold">, </span>width<span style="font-weight: bold">=</span>width<span style="font-weight: bold">)

</span><span style="color: blue; font-weight: bold">def </span>image<span style="font-weight: bold">(</span>pos<span style="font-weight: bold">, </span>file<span style="font-weight: bold">=</span><span style="color: red">"../../blueghost.gif"</span><span style="font-weight: bold">):
    </span>x<span style="font-weight: bold">, </span>y <span style="font-weight: bold">= </span>pos
    <span style="color: green; font-style: italic"># img = PhotoImage(file=file)
    </span><span style="color: blue; font-weight: bold">return </span>_canvas<span style="font-weight: bold">.</span>create_image<span style="font-weight: bold">(</span>x<span style="font-weight: bold">, </span>y<span style="font-weight: bold">, </span>image <span style="font-weight: bold">= </span>Tkinter<span style="font-weight: bold">.</span>PhotoImage<span style="font-weight: bold">(</span>file<span style="font-weight: bold">=</span>file<span style="font-weight: bold">), </span>anchor <span style="font-weight: bold">= </span>Tkinter<span style="font-weight: bold">.</span>NW<span style="font-weight: bold">)
    
    
</span><span style="color: blue; font-weight: bold">def </span>refresh<span style="font-weight: bold">():
      </span>_canvas<span style="font-weight: bold">.</span>update_idletasks<span style="font-weight: bold">()
                                                    
</span><span style="color: blue; font-weight: bold">def </span>moveCircle<span style="font-weight: bold">(</span>id<span style="font-weight: bold">, </span>pos<span style="font-weight: bold">, </span>r<span style="font-weight: bold">, </span>endpoints<span style="font-weight: bold">=</span><span style="color: blue">None</span><span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">global </span>_canvas_x<span style="font-weight: bold">, </span>_canvas_y
    
    x<span style="font-weight: bold">, </span>y <span style="font-weight: bold">= </span>pos
<span style="color: green; font-style: italic">#    x0, x1 = x - r, x + r + 1
#    y0, y1 = y - r, y + r + 1
    </span>x0<span style="font-weight: bold">, </span>x1 <span style="font-weight: bold">= </span>x <span style="font-weight: bold">- </span>r <span style="font-weight: bold">- </span><span style="color: red">1</span><span style="font-weight: bold">, </span>x <span style="font-weight: bold">+ </span>r
    y0<span style="font-weight: bold">, </span>y1 <span style="font-weight: bold">= </span>y <span style="font-weight: bold">- </span>r <span style="font-weight: bold">- </span><span style="color: red">1</span><span style="font-weight: bold">, </span>y <span style="font-weight: bold">+ </span>r
    <span style="color: blue; font-weight: bold">if </span>endpoints <span style="font-weight: bold">== </span><span style="color: blue">None</span><span style="font-weight: bold">:
      </span>e <span style="font-weight: bold">= [</span><span style="color: red">0</span><span style="font-weight: bold">, </span><span style="color: red">359</span><span style="font-weight: bold">]
    </span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
      </span>e <span style="font-weight: bold">= </span>list<span style="font-weight: bold">(</span>endpoints<span style="font-weight: bold">)
    </span><span style="color: blue; font-weight: bold">while </span>e<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">] &gt; </span>e<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">]: </span>e<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">] = </span>e<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">] + </span><span style="color: red">360

    </span>edit<span style="font-weight: bold">(</span>id<span style="font-weight: bold">, (</span><span style="color: red">'start'</span><span style="font-weight: bold">, </span>e<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">]), (</span><span style="color: red">'extent'</span><span style="font-weight: bold">, </span>e<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">] - </span>e<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">]))
    </span>move_to<span style="font-weight: bold">(</span>id<span style="font-weight: bold">, </span>x0<span style="font-weight: bold">, </span>y0<span style="font-weight: bold">)

</span><span style="color: blue; font-weight: bold">def </span>edit<span style="font-weight: bold">(</span>id<span style="font-weight: bold">, *</span>args<span style="font-weight: bold">):
    </span>_canvas<span style="font-weight: bold">.</span>itemconfigure<span style="font-weight: bold">(</span>id<span style="font-weight: bold">, **</span>dict<span style="font-weight: bold">(</span>args<span style="font-weight: bold">))
    
</span><span style="color: blue; font-weight: bold">def </span>text<span style="font-weight: bold">(</span>pos<span style="font-weight: bold">, </span>color<span style="font-weight: bold">, </span>contents<span style="font-weight: bold">, </span>font<span style="font-weight: bold">=</span><span style="color: red">'Helvetica'</span><span style="font-weight: bold">, </span>size<span style="font-weight: bold">=</span><span style="color: red">12</span><span style="font-weight: bold">, </span>style<span style="font-weight: bold">=</span><span style="color: red">'normal'</span><span style="font-weight: bold">, </span>anchor<span style="font-weight: bold">=</span><span style="color: red">"nw"</span><span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">global </span>_canvas_x<span style="font-weight: bold">, </span>_canvas_y
    x<span style="font-weight: bold">, </span>y <span style="font-weight: bold">= </span>pos
    font <span style="font-weight: bold">= (</span>font<span style="font-weight: bold">, </span>str<span style="font-weight: bold">(</span>size<span style="font-weight: bold">), </span>style<span style="font-weight: bold">)
    </span><span style="color: blue; font-weight: bold">return </span>_canvas<span style="font-weight: bold">.</span>create_text<span style="font-weight: bold">(</span>x<span style="font-weight: bold">, </span>y<span style="font-weight: bold">, </span>fill<span style="font-weight: bold">=</span>color<span style="font-weight: bold">, </span>text<span style="font-weight: bold">=</span>contents<span style="font-weight: bold">, </span>font<span style="font-weight: bold">=</span>font<span style="font-weight: bold">, </span>anchor<span style="font-weight: bold">=</span>anchor<span style="font-weight: bold">)

</span><span style="color: blue; font-weight: bold">def </span>changeText<span style="font-weight: bold">(</span>id<span style="font-weight: bold">, </span>newText<span style="font-weight: bold">, </span>font<span style="font-weight: bold">=</span><span style="color: blue">None</span><span style="font-weight: bold">, </span>size<span style="font-weight: bold">=</span><span style="color: red">12</span><span style="font-weight: bold">, </span>style<span style="font-weight: bold">=</span><span style="color: red">'normal'</span><span style="font-weight: bold">):
  </span>_canvas<span style="font-weight: bold">.</span>itemconfigure<span style="font-weight: bold">(</span>id<span style="font-weight: bold">, </span>text<span style="font-weight: bold">=</span>newText<span style="font-weight: bold">)
  </span><span style="color: blue; font-weight: bold">if </span>font <span style="font-weight: bold">!= </span><span style="color: blue">None</span><span style="font-weight: bold">:
    </span>_canvas<span style="font-weight: bold">.</span>itemconfigure<span style="font-weight: bold">(</span>id<span style="font-weight: bold">, </span>font<span style="font-weight: bold">=(</span>font<span style="font-weight: bold">, </span><span style="color: red">'-%d' </span><span style="font-weight: bold">% </span>size<span style="font-weight: bold">, </span>style<span style="font-weight: bold">))

</span><span style="color: blue; font-weight: bold">def </span>changeColor<span style="font-weight: bold">(</span>id<span style="font-weight: bold">, </span>newColor<span style="font-weight: bold">):
  </span>_canvas<span style="font-weight: bold">.</span>itemconfigure<span style="font-weight: bold">(</span>id<span style="font-weight: bold">, </span>fill<span style="font-weight: bold">=</span>newColor<span style="font-weight: bold">)

</span><span style="color: blue; font-weight: bold">def </span>line<span style="font-weight: bold">(</span>here<span style="font-weight: bold">, </span>there<span style="font-weight: bold">, </span>color<span style="font-weight: bold">=</span>formatColor<span style="font-weight: bold">(</span><span style="color: red">0</span><span style="font-weight: bold">, </span><span style="color: red">0</span><span style="font-weight: bold">, </span><span style="color: red">0</span><span style="font-weight: bold">), </span>width<span style="font-weight: bold">=</span><span style="color: red">2</span><span style="font-weight: bold">):
  </span>x0<span style="font-weight: bold">, </span>y0 <span style="font-weight: bold">= </span>here<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">], </span>here<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">]
  </span>x1<span style="font-weight: bold">, </span>y1 <span style="font-weight: bold">= </span>there<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">], </span>there<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">]
  </span><span style="color: blue; font-weight: bold">return </span>_canvas<span style="font-weight: bold">.</span>create_line<span style="font-weight: bold">(</span>x0<span style="font-weight: bold">, </span>y0<span style="font-weight: bold">, </span>x1<span style="font-weight: bold">, </span>y1<span style="font-weight: bold">, </span>fill<span style="font-weight: bold">=</span>color<span style="font-weight: bold">, </span>width<span style="font-weight: bold">=</span>width<span style="font-weight: bold">)

</span><span style="color: green; font-style: italic">##############################################################################
### Keypress handling ########################################################
##############################################################################

# We bind to key-down and key-up events.

</span>_keysdown <span style="font-weight: bold">= {}
</span>_keyswaiting <span style="font-weight: bold">= {}
</span><span style="color: green; font-style: italic"># This holds an unprocessed key release.  We delay key releases by up to
# one call to keys_pressed() to get round a problem with auto repeat.
</span>_got_release <span style="font-weight: bold">= </span><span style="color: blue">None

</span><span style="color: blue; font-weight: bold">def </span>_keypress<span style="font-weight: bold">(</span>event<span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">global </span>_got_release
    <span style="color: green; font-style: italic">#remap_arrows(event)
    </span>_keysdown<span style="font-weight: bold">[</span>event<span style="font-weight: bold">.</span>keysym<span style="font-weight: bold">] = </span><span style="color: red">1
    </span>_keyswaiting<span style="font-weight: bold">[</span>event<span style="font-weight: bold">.</span>keysym<span style="font-weight: bold">] = </span><span style="color: red">1
</span><span style="color: green; font-style: italic">#    print event.char, event.keycode
    </span>_got_release <span style="font-weight: bold">= </span><span style="color: blue">None

</span><span style="color: blue; font-weight: bold">def </span>_keyrelease<span style="font-weight: bold">(</span>event<span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">global </span>_got_release
    <span style="color: green; font-style: italic">#remap_arrows(event)
    </span><span style="color: blue; font-weight: bold">try</span><span style="font-weight: bold">:
      </span><span style="color: blue; font-weight: bold">del </span>_keysdown<span style="font-weight: bold">[</span>event<span style="font-weight: bold">.</span>keysym<span style="font-weight: bold">]
    </span><span style="color: blue; font-weight: bold">except</span><span style="font-weight: bold">:
      </span><span style="color: blue; font-weight: bold">pass
    </span>_got_release <span style="font-weight: bold">= </span><span style="color: red">1
    
</span><span style="color: blue; font-weight: bold">def </span>remap_arrows<span style="font-weight: bold">(</span>event<span style="font-weight: bold">):
    </span><span style="color: green; font-style: italic"># TURN ARROW PRESSES INTO LETTERS (SHOULD BE IN KEYBOARD AGENT)
    </span><span style="color: blue; font-weight: bold">if </span>event<span style="font-weight: bold">.</span>char <span style="color: blue; font-weight: bold">in </span><span style="font-weight: bold">[</span><span style="color: red">'a'</span><span style="font-weight: bold">, </span><span style="color: red">'s'</span><span style="font-weight: bold">, </span><span style="color: red">'d'</span><span style="font-weight: bold">, </span><span style="color: red">'w'</span><span style="font-weight: bold">]:
      </span><span style="color: blue; font-weight: bold">return
    if </span>event<span style="font-weight: bold">.</span>keycode <span style="color: blue; font-weight: bold">in </span><span style="font-weight: bold">[</span><span style="color: red">37</span><span style="font-weight: bold">, </span><span style="color: red">101</span><span style="font-weight: bold">]: </span><span style="color: green; font-style: italic"># LEFT ARROW (win / x)
      </span>event<span style="font-weight: bold">.</span>char <span style="font-weight: bold">= </span><span style="color: red">'a'
    </span><span style="color: blue; font-weight: bold">if </span>event<span style="font-weight: bold">.</span>keycode <span style="color: blue; font-weight: bold">in </span><span style="font-weight: bold">[</span><span style="color: red">38</span><span style="font-weight: bold">, </span><span style="color: red">99</span><span style="font-weight: bold">]: </span><span style="color: green; font-style: italic"># UP ARROW
      </span>event<span style="font-weight: bold">.</span>char <span style="font-weight: bold">= </span><span style="color: red">'w'
    </span><span style="color: blue; font-weight: bold">if </span>event<span style="font-weight: bold">.</span>keycode <span style="color: blue; font-weight: bold">in </span><span style="font-weight: bold">[</span><span style="color: red">39</span><span style="font-weight: bold">, </span><span style="color: red">102</span><span style="font-weight: bold">]: </span><span style="color: green; font-style: italic"># RIGHT ARROW
      </span>event<span style="font-weight: bold">.</span>char <span style="font-weight: bold">= </span><span style="color: red">'d'
    </span><span style="color: blue; font-weight: bold">if </span>event<span style="font-weight: bold">.</span>keycode <span style="color: blue; font-weight: bold">in </span><span style="font-weight: bold">[</span><span style="color: red">40</span><span style="font-weight: bold">, </span><span style="color: red">104</span><span style="font-weight: bold">]: </span><span style="color: green; font-style: italic"># DOWN ARROW
      </span>event<span style="font-weight: bold">.</span>char <span style="font-weight: bold">= </span><span style="color: red">'s'

</span><span style="color: blue; font-weight: bold">def </span>_clear_keys<span style="font-weight: bold">(</span>event<span style="font-weight: bold">=</span><span style="color: blue">None</span><span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">global </span>_keysdown<span style="font-weight: bold">, </span>_got_release<span style="font-weight: bold">, </span>_keyswaiting
    _keysdown <span style="font-weight: bold">= {}
    </span>_keyswaiting <span style="font-weight: bold">= {}
    </span>_got_release <span style="font-weight: bold">= </span><span style="color: blue">None

</span><span style="color: blue; font-weight: bold">def </span>keys_pressed<span style="font-weight: bold">(</span>d_o_e<span style="font-weight: bold">=</span>Tkinter<span style="font-weight: bold">.</span>tkinter<span style="font-weight: bold">.</span>dooneevent<span style="font-weight: bold">,
                 </span>d_w<span style="font-weight: bold">=</span>Tkinter<span style="font-weight: bold">.</span>tkinter<span style="font-weight: bold">.</span>DONT_WAIT<span style="font-weight: bold">):
    </span>d_o_e<span style="font-weight: bold">(</span>d_w<span style="font-weight: bold">)
    </span><span style="color: blue; font-weight: bold">if </span>_got_release<span style="font-weight: bold">:
      </span>d_o_e<span style="font-weight: bold">(</span>d_w<span style="font-weight: bold">)
    </span><span style="color: blue; font-weight: bold">return </span>_keysdown<span style="font-weight: bold">.</span>keys<span style="font-weight: bold">()
  
</span><span style="color: blue; font-weight: bold">def </span>keys_waiting<span style="font-weight: bold">():
  </span><span style="color: blue; font-weight: bold">global </span>_keyswaiting
  keys <span style="font-weight: bold">= </span>_keyswaiting<span style="font-weight: bold">.</span>keys<span style="font-weight: bold">()
  </span>_keyswaiting <span style="font-weight: bold">= {}
  </span><span style="color: blue; font-weight: bold">return </span>keys

<span style="color: green; font-style: italic"># Block for a list of keys...

</span><span style="color: blue; font-weight: bold">def </span>wait_for_keys<span style="font-weight: bold">():
    </span>keys <span style="font-weight: bold">= []
    </span><span style="color: blue; font-weight: bold">while </span>keys <span style="font-weight: bold">== []:
        </span>keys <span style="font-weight: bold">= </span>keys_pressed<span style="font-weight: bold">()
        </span>sleep<span style="font-weight: bold">(</span><span style="color: red">0.05</span><span style="font-weight: bold">)
    </span><span style="color: blue; font-weight: bold">return </span>keys

<span style="color: blue; font-weight: bold">def </span>remove_from_screen<span style="font-weight: bold">(</span>x<span style="font-weight: bold">,
                       </span>d_o_e<span style="font-weight: bold">=</span>Tkinter<span style="font-weight: bold">.</span>tkinter<span style="font-weight: bold">.</span>dooneevent<span style="font-weight: bold">,
                       </span>d_w<span style="font-weight: bold">=</span>Tkinter<span style="font-weight: bold">.</span>tkinter<span style="font-weight: bold">.</span>DONT_WAIT<span style="font-weight: bold">):
    </span>_canvas<span style="font-weight: bold">.</span>delete<span style="font-weight: bold">(</span>x<span style="font-weight: bold">)
    </span>d_o_e<span style="font-weight: bold">(</span>d_w<span style="font-weight: bold">)

</span><span style="color: blue; font-weight: bold">def </span>_adjust_coords<span style="font-weight: bold">(</span>coord_list<span style="font-weight: bold">, </span>x<span style="font-weight: bold">, </span>y<span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">for </span>i <span style="color: blue; font-weight: bold">in </span>range<span style="font-weight: bold">(</span><span style="color: red">0</span><span style="font-weight: bold">, </span>len<span style="font-weight: bold">(</span>coord_list<span style="font-weight: bold">), </span><span style="color: red">2</span><span style="font-weight: bold">):
        </span>coord_list<span style="font-weight: bold">[</span>i<span style="font-weight: bold">] = </span>coord_list<span style="font-weight: bold">[</span>i<span style="font-weight: bold">] + </span>x
        coord_list<span style="font-weight: bold">[</span>i <span style="font-weight: bold">+ </span><span style="color: red">1</span><span style="font-weight: bold">] = </span>coord_list<span style="font-weight: bold">[</span>i <span style="font-weight: bold">+ </span><span style="color: red">1</span><span style="font-weight: bold">] + </span>y
    <span style="color: blue; font-weight: bold">return </span>coord_list

<span style="color: blue; font-weight: bold">def </span>move_to<span style="font-weight: bold">(</span>object<span style="font-weight: bold">, </span>x<span style="font-weight: bold">, </span>y<span style="font-weight: bold">=</span><span style="color: blue">None</span><span style="font-weight: bold">,
            </span>d_o_e<span style="font-weight: bold">=</span>Tkinter<span style="font-weight: bold">.</span>tkinter<span style="font-weight: bold">.</span>dooneevent<span style="font-weight: bold">,
            </span>d_w<span style="font-weight: bold">=</span>Tkinter<span style="font-weight: bold">.</span>tkinter<span style="font-weight: bold">.</span>DONT_WAIT<span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">if </span>y <span style="color: blue; font-weight: bold">is </span><span style="color: blue">None</span><span style="font-weight: bold">:
        </span><span style="color: blue; font-weight: bold">try</span><span style="font-weight: bold">: </span>x<span style="font-weight: bold">, </span>y <span style="font-weight: bold">= </span>x
        <span style="color: blue; font-weight: bold">except</span><span style="font-weight: bold">: </span><span style="color: blue; font-weight: bold">raise  </span><span style="color: red">'incomprehensible coordinates' 
        
    </span>horiz <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">True
    </span>newCoords <span style="font-weight: bold">= []
    </span>current_x<span style="font-weight: bold">, </span>current_y <span style="font-weight: bold">= </span>_canvas<span style="font-weight: bold">.</span>coords<span style="font-weight: bold">(</span>object<span style="font-weight: bold">)[</span><span style="color: red">0</span><span style="font-weight: bold">:</span><span style="color: red">2</span><span style="font-weight: bold">] </span><span style="color: green; font-style: italic"># first point
    </span><span style="color: blue; font-weight: bold">for </span>coord <span style="color: blue; font-weight: bold">in  </span>_canvas<span style="font-weight: bold">.</span>coords<span style="font-weight: bold">(</span>object<span style="font-weight: bold">):
      </span><span style="color: blue; font-weight: bold">if </span>horiz<span style="font-weight: bold">:  
        </span>inc <span style="font-weight: bold">= </span>x <span style="font-weight: bold">- </span>current_x
      <span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:      
        </span>inc <span style="font-weight: bold">= </span>y <span style="font-weight: bold">- </span>current_y
      horiz <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">not </span>horiz
      
      newCoords<span style="font-weight: bold">.</span>append<span style="font-weight: bold">(</span>coord <span style="font-weight: bold">+ </span>inc<span style="font-weight: bold">)
    
    </span>_canvas<span style="font-weight: bold">.</span>coords<span style="font-weight: bold">(</span>object<span style="font-weight: bold">, *</span>newCoords<span style="font-weight: bold">)
    </span>d_o_e<span style="font-weight: bold">(</span>d_w<span style="font-weight: bold">)
    
</span><span style="color: blue; font-weight: bold">def </span>move_by<span style="font-weight: bold">(</span>object<span style="font-weight: bold">, </span>x<span style="font-weight: bold">, </span>y<span style="font-weight: bold">=</span><span style="color: blue">None</span><span style="font-weight: bold">,
            </span>d_o_e<span style="font-weight: bold">=</span>Tkinter<span style="font-weight: bold">.</span>tkinter<span style="font-weight: bold">.</span>dooneevent<span style="font-weight: bold">,
            </span>d_w<span style="font-weight: bold">=</span>Tkinter<span style="font-weight: bold">.</span>tkinter<span style="font-weight: bold">.</span>DONT_WAIT<span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">if </span>y <span style="color: blue; font-weight: bold">is </span><span style="color: blue">None</span><span style="font-weight: bold">:
        </span><span style="color: blue; font-weight: bold">try</span><span style="font-weight: bold">: </span>x<span style="font-weight: bold">, </span>y <span style="font-weight: bold">= </span>x
        <span style="color: blue; font-weight: bold">except</span><span style="font-weight: bold">: </span><span style="color: blue; font-weight: bold">raise </span>Exception<span style="font-weight: bold">, </span><span style="color: red">'incomprehensible coordinates' 
    
    </span>horiz <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">True
    </span>newCoords <span style="font-weight: bold">= []
    </span><span style="color: blue; font-weight: bold">for </span>coord <span style="color: blue; font-weight: bold">in  </span>_canvas<span style="font-weight: bold">.</span>coords<span style="font-weight: bold">(</span>object<span style="font-weight: bold">):
      </span><span style="color: blue; font-weight: bold">if </span>horiz<span style="font-weight: bold">:  
        </span>inc <span style="font-weight: bold">= </span>x
      <span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:      
        </span>inc <span style="font-weight: bold">= </span>y
      horiz <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">not </span>horiz
      
      newCoords<span style="font-weight: bold">.</span>append<span style="font-weight: bold">(</span>coord <span style="font-weight: bold">+ </span>inc<span style="font-weight: bold">)
      
    </span>_canvas<span style="font-weight: bold">.</span>coords<span style="font-weight: bold">(</span>object<span style="font-weight: bold">, *</span>newCoords<span style="font-weight: bold">)
    </span>d_o_e<span style="font-weight: bold">(</span>d_w<span style="font-weight: bold">)
    
</span><span style="color: blue; font-weight: bold">def </span>writePostscript<span style="font-weight: bold">(</span>filename<span style="font-weight: bold">):
  </span><span style="color: red">"Writes the current canvas to a postscript file."    
  </span>psfile <span style="font-weight: bold">= </span>file<span style="font-weight: bold">(</span>filename<span style="font-weight: bold">, </span><span style="color: red">'w'</span><span style="font-weight: bold">)
  </span>psfile<span style="font-weight: bold">.</span>write<span style="font-weight: bold">(</span>_canvas<span style="font-weight: bold">.</span>postscript<span style="font-weight: bold">(</span>pageanchor<span style="font-weight: bold">=</span><span style="color: red">'sw'</span><span style="font-weight: bold">,
                   </span>y<span style="font-weight: bold">=</span><span style="color: red">'0.c'</span><span style="font-weight: bold">, 
                   </span>x<span style="font-weight: bold">=</span><span style="color: red">'0.c'</span><span style="font-weight: bold">))
  </span>psfile<span style="font-weight: bold">.</span>close<span style="font-weight: bold">()
  
</span>ghost_shape <span style="font-weight: bold">= [                
    (</span><span style="color: red">0</span><span style="font-weight: bold">, - </span><span style="color: red">0.5</span><span style="font-weight: bold">),
    (</span><span style="color: red">0.25</span><span style="font-weight: bold">, - </span><span style="color: red">0.75</span><span style="font-weight: bold">),
    (</span><span style="color: red">0.5</span><span style="font-weight: bold">, - </span><span style="color: red">0.5</span><span style="font-weight: bold">),
    (</span><span style="color: red">0.75</span><span style="font-weight: bold">, - </span><span style="color: red">0.75</span><span style="font-weight: bold">),
    (</span><span style="color: red">0.75</span><span style="font-weight: bold">, </span><span style="color: red">0.5</span><span style="font-weight: bold">),
    (</span><span style="color: red">0.5</span><span style="font-weight: bold">, </span><span style="color: red">0.75</span><span style="font-weight: bold">),
    (- </span><span style="color: red">0.5</span><span style="font-weight: bold">, </span><span style="color: red">0.75</span><span style="font-weight: bold">),
    (- </span><span style="color: red">0.75</span><span style="font-weight: bold">, </span><span style="color: red">0.5</span><span style="font-weight: bold">),
    (- </span><span style="color: red">0.75</span><span style="font-weight: bold">, - </span><span style="color: red">0.75</span><span style="font-weight: bold">),
    (- </span><span style="color: red">0.5</span><span style="font-weight: bold">, - </span><span style="color: red">0.5</span><span style="font-weight: bold">),
    (- </span><span style="color: red">0.25</span><span style="font-weight: bold">, - </span><span style="color: red">0.75</span><span style="font-weight: bold">)
  ]

</span><span style="color: blue; font-weight: bold">if </span>__name__ <span style="font-weight: bold">== </span><span style="color: red">'__main__'</span><span style="font-weight: bold">:
  </span>begin_graphics<span style="font-weight: bold">()
  </span>clear_screen<span style="font-weight: bold">()
  </span>ghost_shape <span style="font-weight: bold">= [(</span>x <span style="font-weight: bold">* </span><span style="color: red">10 </span><span style="font-weight: bold">+ </span><span style="color: red">20</span><span style="font-weight: bold">, </span>y <span style="font-weight: bold">* </span><span style="color: red">10 </span><span style="font-weight: bold">+ </span><span style="color: red">20</span><span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">for </span>x<span style="font-weight: bold">, </span>y <span style="color: blue; font-weight: bold">in </span>ghost_shape<span style="font-weight: bold">]
  </span>g <span style="font-weight: bold">= </span>polygon<span style="font-weight: bold">(</span>ghost_shape<span style="font-weight: bold">, </span>formatColor<span style="font-weight: bold">(</span><span style="color: red">1</span><span style="font-weight: bold">, </span><span style="color: red">1</span><span style="font-weight: bold">, </span><span style="color: red">1</span><span style="font-weight: bold">))
  </span>move_to<span style="font-weight: bold">(</span>g<span style="font-weight: bold">, (</span><span style="color: red">50</span><span style="font-weight: bold">, </span><span style="color: red">50</span><span style="font-weight: bold">))
  </span>circle<span style="font-weight: bold">((</span><span style="color: red">150</span><span style="font-weight: bold">, </span><span style="color: red">150</span><span style="font-weight: bold">), </span><span style="color: red">20</span><span style="font-weight: bold">, </span>formatColor<span style="font-weight: bold">(</span><span style="color: red">0.7</span><span style="font-weight: bold">, </span><span style="color: red">0.3</span><span style="font-weight: bold">, </span><span style="color: red">0.0</span><span style="font-weight: bold">), </span>endpoints<span style="font-weight: bold">=[</span><span style="color: red">15</span><span style="font-weight: bold">, - </span><span style="color: red">15</span><span style="font-weight: bold">])
  </span>sleep<span style="font-weight: bold">(</span><span style="color: red">2</span><span style="font-weight: bold">)</span>
  </pre>
  </body>
  </html>
  